﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    {
         
        [ChildActionOnly]
        public ActionResult MTWZ04(string returnUrl)
        {
            ModelState.Clear();
            if (WBBS == null)
            {
                WB2WH();
            }
            WBBSTModel mx = new WBBSTModel();
            ViewBag.WBBS = WBBS;
            try
            {
                var result = WBBS.Where(p => p.LB == MABF.CWBB.SLB).OrderByDescending(p => p.WBBSTKNR);
                int remain = result.Count();
                if (remain == 0)
                    VHP.VWBW.POSTCHG();
                mx.REMARK = "待入库:" + remain.ToString();
                mx.WBBSTKNR = "已入库:" + (WBBS.Count() - result.Count()).ToString();
            }
            catch (Exception ex)
            {
                mx.REMARK = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }   
            ViewBag.Model = mx;
            ViewBag.ReturnUrl = returnUrl;
            return PartialView(mx);
        }
    

        /// <summary>
        /// 装载所有布卷
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult STWZ04(string returnUrl)
        {
            //插入到一个数据库表，然后批量进行移动记录，记录下来时间戳和申请的ID号码

            ModelState.Clear();
            MABF.CWBB.SLO = "01";
            MABF.CWBB.SLB = "CW";
            MABF.CWBB.LO = "01";
            MABF.CWBB.LB = "KO";
            MABF.CWBB.LP = "        ";

            MABF.CWBB.WHP = MABF.CWBB.LO + MABF.CWBB.LB + MABF.CWBB.LP;
            MABF.TERMNO = app.TERMNO;
            MABF.STAFF = CUSER;
            MABF.REMARK = "请扫描";
            if (WBBS == null)
            {
                WB2WH();
            }
            if (WBBS == null)
            {
                MABF.CWBB.REMARK = "没有可以扫描的布卷，请核对";
                return PartialView(MABF.CWBB);
            }

            if (LMODEL != null)
            {
                if (!LMODEL.IsDataValid)
                {
                    return PartialView(LMODEL);
                }
            }
            if (LMODEL != null)
            {
                MABF.CWBB.REMARK = ((WBBSTModel)LMODEL).WBBSTKNR + LMODEL.REMARK;
            }
            return PartialView(MABF.CWBB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult STWZ04(WBBSTModel model, string returnUrl)
        {
            MOVE2KO(model);//发货操做
            LMODEL = model;
            ModelState.Clear();
            return RedirectToAction(MAPP, MAPP);
        }
        [ChildActionOnly]
        public ActionResult STWZ04ALL(string returnUrl)
        {
            //插入到一个数据库表，然后批量进行移动记录，记录下来时间戳和申请的ID号码

            ModelState.Clear();
            MABF.CWBB.SLO = "01";
            MABF.CWBB.SLB = "CW";
            MABF.CWBB.LO = "01";
            MABF.CWBB.LB = "KO";
            MABF.CWBB.LP = "        ";
           
            MABF.CWBB.WHP = MABF.CWBB.LO + MABF.CWBB.LB + MABF.CWBB.LP;
            MABF.TERMNO = app.TERMNO;
            MABF.STAFF = CUSER;
            MABF.REMARK = "请扫描";
            if (WBBS == null)
            {
                WB2WH();
            }
            if (WBBS == null)
            {
                MABF.CWBB.REMARK = "没有可以扫描的布卷，请核对";
                return PartialView(MABF.CWBB);
            }

            if (LMODEL != null)
            {
                if (!LMODEL.IsDataValid)
                {
                    return PartialView(LMODEL);
                }
            }
            if (LMODEL != null)
            {
                MABF.CWBB.REMARK = ((WBBSTModel)LMODEL).WBBSTKNR + LMODEL.REMARK;
            }
            return PartialView(MABF.CWBB);
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult STWZ04ALL(WBBSTModel model, string returnUrl)
        {
            SHIPALL(model);//发货操做
            LMODEL = model;
            ModelState.Clear();
            return RedirectToAction(MAPP, MAPP);
        }

        public void MOVE2KO(WBBSTModel model)
        {
            if (model == null)
                return;
            if (WBBS == null)
                return; 
            model.WBBSTKNR = Helper.Left(model.WBBSTKNR.PadRight(12), 12);
            var result = WBBS.Where(p => p.WBBSTKNR == model.WBBSTKNR && p.LO == MABF.CWBB.LO && p.LB == MABF.CWBB.LB).OrderByDescending(p => p.WBBSTKNR);
            if (result.Count() > 0)
            {
                model.IsDataValid = false;
                model.REMARK = "重复发货";
                return;
            }
            result = WBBS.Where(p => p.WBBSTKNR == model.WBBSTKNR).OrderByDescending(p => p.WBBSTKNR);
            if (result.Count() == 0)
            {
                model.IsDataValid = false;
                model.REMARK = "不在范围内";
                return;
            }
            model.IsDataValid = true;  
            filter = "WBBSTKNR='" + model.WBBSTKNR + "'";
            rows = WBLIST.Select(filter);
            foreach (DataRow _dwWB in rows)
            {
                VHP.VWBB.MOVE2KO(_dwWB, MABF.CWBB.LO, MABF.CWBB.LB, MABF.CWBB.LP, RUNID);
                M2WB(_dwWB);
                model.REMARK = "完成(" + _dwWB["WBBPACKNR"].ToString() + ")";
            }
        }

        public void SHIPALL(WBBSTModel model)
        {
            if (model == null)
                return;
            if (WBBS == null)
                return;
            string _lo = model.WHP.Substring(0, 2);
            string _lb = model.WHP.Substring(2, 2);
            string _lp = model.WHP.Substring(4, 8);
           
            model.IsDataValid = true;
            string filter =   "WBBLB<>'"+_lb+"'";
            DataRow[] rows = WBLIST.Select(filter);
            foreach (DataRow _dwWB in rows)
            {
                VHP.VWBB.MOVE2KO(_dwWB, _lo, _lb, _lp, RUNID);
                M2WB(_dwWB);                
            }
            model.REMARK = "完成(" + rows.Length + ")";
        }
    }
}
